# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Description:
#   Python bindings for MLIR.
#
# We define separate filegroups for files in different directories so
# that downstream users can mirror the tree in their own py_library() rules.

load("//mlir:tblgen.bzl", "gentbl_filegroup", "td_library")

package(
    default_visibility = ["//visibility:public"],
    licenses = ["notice"],
)

##---------------------------------------------------------------------------##
# Core IR modules.
##---------------------------------------------------------------------------##

filegroup(
    name = "DialectCorePyFiles",
    srcs = [
        "mlir/dialects/_ods_common.py",
    ],
)

filegroup(
    name = "ExecutionEnginePyFiles",
    srcs = [
        "mlir/execution_engine.py",
    ],
)

filegroup(
    name = "ExecutionEnginePyIFiles",
    srcs = [
        "mlir/_mlir_libs/_mlirExecutionEngine.pyi",
    ],
)

filegroup(
    name = "IRPyFiles",
    srcs = [
        "mlir/ir.py",
    ],
)

filegroup(
    name = "IRPyIFiles",
    srcs = [
        "mlir/_mlir_libs/_mlir/__init__.pyi",
        "mlir/_mlir_libs/_mlir/ir.pyi",
    ],
)

filegroup(
    name = "MlirLibsPyFiles",
    srcs = [
        "mlir/_mlir_libs/__init__.py",
    ],
)

filegroup(
    name = "PassManagerPyFiles",
    srcs = [
        "mlir/passmanager.py",
    ],
)

filegroup(
    name = "PassManagerPyIFiles",
    srcs = [
        "mlir/_mlir_libs/_mlir/passmanager.pyi",
    ],
)

filegroup(
    name = "RuntimePyFiles",
    srcs = glob([
        "mlir/runtime/*.py",
    ]),
)

##---------------------------------------------------------------------------##
# Builtin dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "BuiltinOpsPyTdFiles",
    srcs = [
        "mlir/dialects/BuiltinOps.td",
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:BuiltinDialectTdFiles",
        "//mlir:FunctionInterfacesTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "BuiltinOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=builtin",
            ],
            "mlir/dialects/_builtin_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/BuiltinOps.td",
    deps = [
        ":BuiltinOpsPyTdFiles",
    ],
)

filegroup(
    name = "BuiltinOpsPyFiles",
    srcs = [
        "mlir/dialects/_builtin_ops_ext.py",
        "mlir/dialects/builtin.py",
        ":BuiltinOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Linalg dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "LinalgOpsPyTdFiles",
    srcs = [
        "mlir/dialects/LinalgOps.td",
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:LinalgOpsTdFiles",
        "//mlir:LinalgStructuredOpsTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "LinalgOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=linalg",
            ],
            "mlir/dialects/_linalg_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/LinalgOps.td",
    deps = [
        ":LinalgOpsPyTdFiles",
    ],
)

filegroup(
    name = "LinalgOpsPyFiles",
    srcs = [
        "mlir/dialects/_linalg_ops_ext.py",
        ":LinalgOpsPyGen",
    ],
)

filegroup(
    name = "LinalgOpsPackagePyFiles",
    srcs = glob(["mlir/dialects/linalg/*.py"]),
)

filegroup(
    name = "LinalgOpsPackageOpDSLPyFiles",
    srcs = glob(["mlir/dialects/linalg/opdsl/*.py"]),
)

filegroup(
    name = "LinalgOpsPackageOpDSLLangPyFiles",
    srcs = glob(["mlir/dialects/linalg/opdsl/lang/*.py"]),
)

filegroup(
    name = "LinalgOpsPackageOpDSLOpsPyFiles",
    srcs = glob(["mlir/dialects/linalg/opdsl/ops/*.py"]),
)

filegroup(
    name = "LinalgOpsPackagePassesPyFiles",
    srcs = glob(["mlir/dialects/linalg/passes/*.py"]),
)

##---------------------------------------------------------------------------##
# Arithmetic dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "ArithmeticOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:ArithmeticOpsTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "ArithmeticOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=arith",
            ],
            "mlir/dialects/_arith_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/ArithmeticOps.td",
    deps = [
        ":ArithmeticOpsPyTdFiles",
    ],
)

filegroup(
    name = "ArithmeticOpsPyFiles",
    srcs = [
        "mlir/dialects/_arith_ops_ext.py",
        "mlir/dialects/arith.py",
        ":ArithmeticOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Bufferization dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "BufferizationOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:BufferizableOpInterfaceTdFiles",
        "//mlir:BufferizationOpsTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "BufferizationOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=bufferization",
            ],
            "mlir/dialects/_bufferization_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/BufferizationOps.td",
    deps = [
        ":BufferizationOpsPyTdFiles",
    ],
)

filegroup(
    name = "BufferizationOpsPyFiles",
    srcs = [
        "mlir/dialects/_bufferization_ops_ext.py",
        "mlir/dialects/bufferization.py",
        ":BufferizationOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Complex dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "ComplexOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:ComplexOpsTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "ComplexOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=complex",
            ],
            "mlir/dialects/_complex_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/ComplexOps.td",
    deps = [
        ":ComplexOpsPyTdFiles",
    ],
)

filegroup(
    name = "ComplexOpsPyFiles",
    srcs = [
        "mlir/dialects/complex.py",
        ":ComplexOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# ControlFlow dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "ControlFlowOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:ControlFlowOpsTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "ControlFlowOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=cf",
            ],
            "mlir/dialects/_cf_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/ControlFlowOps.td",
    deps = [
        ":ControlFlowOpsPyTdFiles",
    ],
)

filegroup(
    name = "ControlFlowOpsPyFiles",
    srcs = [
        "mlir/dialects/cf.py",
        ":ControlFlowOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Math dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "MathOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:MathOpsTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "MathOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=math",
            ],
            "mlir/dialects/_math_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/MathOps.td",
    deps = [
        ":MathOpsPyTdFiles",
    ],
)

filegroup(
    name = "MathOpsPyFiles",
    srcs = [
        "mlir/dialects/math.py",
        ":MathOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# MemRef dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "MemRefOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:MemRefOpsTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "MemRefOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=memref",
            ],
            "mlir/dialects/_memref_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/MemRefOps.td",
    deps = [
        ":MemRefOpsPyTdFiles",
    ],
)

filegroup(
    name = "MemRefOpsPyFiles",
    srcs = [
        "mlir/dialects/_memref_ops_ext.py",
        "mlir/dialects/memref.py",
        ":MemRefOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# MLProgram dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "MLProgramOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:MLProgramOpsTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "MLProgramOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=ml_program",
            ],
            "mlir/dialects/_ml_program_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/MLProgramOps.td",
    deps = [
        ":MLProgramOpsPyTdFiles",
    ],
)

filegroup(
    name = "MLProgramOpsPyFiles",
    srcs = [
        "mlir/dialects/_ml_program_ops_ext.py",
        "mlir/dialects/ml_program.py",
        ":MLProgramOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# PDL dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "PDLPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:OpBaseTdFiles",
        "//mlir:PDLDialectTdFiles",
    ],
)

gentbl_filegroup(
    name = "PDLPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=pdl",
            ],
            "mlir/dialects/_pdl_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/PDLOps.td",
    deps = [
        ":PDLPyTdFiles",
    ],
)

filegroup(
    name = "PDLPyIFiles",
    srcs = [
        "mlir/_mlir_libs/_mlir/dialects/pdl.pyi",
    ],
)

filegroup(
    name = "PDLPyFiles",
    srcs = [
        "mlir/dialects/_pdl_ops_ext.py",
        "mlir/dialects/pdl.py",
        ":PDLPyGen",
    ],
)


##---------------------------------------------------------------------------##
# PythonTest dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "PythonTestPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:InferTypeOpInterfaceTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "PythonTestPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=python_test",
            ],
            "mlir/dialects/_python_test_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "//mlir/test/python:python_test_ops.td",
    deps = [
        ":PythonTestPyTdFiles",
    ],
)

filegroup(
    name = "PythonTestPyFiles",
    srcs = [
        "mlir/dialects/python_test.py",
        ":PythonTestPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Quant dialect.
##---------------------------------------------------------------------------##

filegroup(
    name = "QuantPyIFiles",
    srcs = [
        "mlir/_mlir_libs/_mlir/dialects/quant.pyi",
    ],
)

filegroup(
    name = "QuantPyFiles",
    srcs = [
        "mlir/dialects/quant.py",
    ],
)

##---------------------------------------------------------------------------##
# SCF dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "SCFPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:OpBaseTdFiles",
        "//mlir:SCFTdFiles",
    ],
)

gentbl_filegroup(
    name = "SCFPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=scf",
            ],
            "mlir/dialects/_scf_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/SCFOps.td",
    deps = [
        ":SCFPyTdFiles",
    ],
)

filegroup(
    name = "SCFPyFiles",
    srcs = [
        "mlir/dialects/_scf_ops_ext.py",
        "mlir/dialects/scf.py",
        ":SCFPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Shape dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "ShapeOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:OpBaseTdFiles",
        "//mlir:ShapeOpsTdFiles",
    ],
)

gentbl_filegroup(
    name = "ShapeOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=shape",
            ],
            "mlir/dialects/_shape_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/ShapeOps.td",
    deps = [
        ":ShapeOpsPyTdFiles",
    ],
)

filegroup(
    name = "ShapeOpsPyFiles",
    srcs = [
        "mlir/dialects/shape.py",
        ":ShapeOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Func dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "FuncPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:FuncTdFiles",
        "//mlir:OpBaseTdFiles",
    ],
)

gentbl_filegroup(
    name = "FuncPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=func",
            ],
            "mlir/dialects/_func_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/FuncOps.td",
    deps = [
        ":FuncPyTdFiles",
    ],
)

filegroup(
    name = "FuncPyFiles",
    srcs = [
        "mlir/dialects/_func_ops_ext.py",
        "mlir/dialects/func.py",
        ":FuncPyGen",
    ],
)

##---------------------------------------------------------------------------##
# SparseTensor dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "SparseTensorOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:OpBaseTdFiles",
        "//mlir:SparseTensorTdFiles",
    ],
)

gentbl_filegroup(
    name = "SparseTensorOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=sparse_tensor",
            ],
            "mlir/dialects/_sparse_tensor_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/SparseTensorOps.td",
    deps = [
        ":SparseTensorOpsPyTdFiles",
    ],
)

filegroup(
    name = "SparseTensorOpsPyFiles",
    srcs = [
        "mlir/dialects/sparse_tensor.py",
        ":SparseTensorOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Tensor dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "TensorOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:OpBaseTdFiles",
        "//mlir:TensorOpsTdFiles",
    ],
)

gentbl_filegroup(
    name = "TensorOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=tensor",
            ],
            "mlir/dialects/_tensor_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/TensorOps.td",
    deps = [
        ":TensorOpsPyTdFiles",
    ],
)

filegroup(
    name = "TensorOpsPyFiles",
    srcs = [
        "mlir/dialects/tensor.py",
        ":TensorOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Tosa dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "TosaOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:OpBaseTdFiles",
        "//mlir:TosaDialectTdFiles",
    ],
)

gentbl_filegroup(
    name = "TosaOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=tosa",
            ],
            "mlir/dialects/_tosa_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/TosaOps.td",
    deps = [
        ":TosaOpsPyTdFiles",
    ],
)

filegroup(
    name = "TosaOpsPyFiles",
    srcs = [
        "mlir/dialects/tosa.py",
        ":TosaOpsPyGen",
    ],
)

##---------------------------------------------------------------------------##
# Transform dialect and extensions.
##---------------------------------------------------------------------------##

td_library(
    name = "TransformOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    deps = [
        "//mlir:OpBaseTdFiles",
        "//mlir:TransformDialectTdFiles",
    ],
)

gentbl_filegroup(
    name = "TransformOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=transform",
            ],
            "mlir/dialects/_transform_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/TransformOps.td",
    deps = [
        ":TransformOpsPyTdFiles",
    ],
)

gentbl_filegroup(
    name = "StructuredTransformOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=transform",
                "-dialect-extension=structured_transform",
            ],
            "mlir/dialects/_structured_transform_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/LinalgStructuredTransformOps.td",
    deps = [
        ":TransformOpsPyTdFiles",
        "//mlir:LinalgTransformOpsTdFiles",
    ],
)

gentbl_filegroup(
    name = "LoopTransformOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=transform",
                "-dialect-extension=loop_transform",
            ],
            "mlir/dialects/_loop_transform_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/SCFLoopTransformOps.td",
    deps = [
        ":TransformOpsPyTdFiles",
        "//mlir:SCFTransformOpsTdFiles",
    ],
)

filegroup(
    name = "TransformOpsPyFiles",
    srcs = [
        "mlir/dialects/_loop_transform_ops_ext.py",
        "mlir/dialects/_structured_transform_ops_ext.py",
        "mlir/dialects/_transform_ops_ext.py",
        ":LoopTransformOpsPyGen",
        ":StructuredTransformOpsPyGen",
        ":TransformOpsPyGen",
    ],
)

filegroup(
    name = "TransformOpsPackagePyFiles",
    srcs = glob(["mlir/dialects/transform/*.py"]),
)

##---------------------------------------------------------------------------##
# Vector dialect.
##---------------------------------------------------------------------------##

td_library(
    name = "VectorOpsPyTdFiles",
    srcs = [
        "//mlir:include/mlir/Bindings/Python/Attributes.td",
    ],
    includes = ["../include"],
    deps = [
        "//mlir:OpBaseTdFiles",
        "//mlir:VectorOpsTdFiles",
    ],
)

gentbl_filegroup(
    name = "VectorOpsPyGen",
    tbl_outs = [
        (
            [
                "-gen-python-op-bindings",
                "-bind-dialect=vector",
            ],
            "mlir/dialects/_vector_ops_gen.py",
        ),
    ],
    tblgen = "//mlir:mlir-tblgen",
    td_file = "mlir/dialects/VectorOps.td",
    deps = [
        ":VectorOpsPyTdFiles",
    ],
)

filegroup(
    name = "VectorOpsPyFiles",
    srcs = [
        "mlir/dialects/vector.py",
        ":VectorOpsPyGen",
    ],
)
